package com.yc.jiushujie.jsj_user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.common.bean.Result;
import com.yc.jiushujie.entity.JsjBook;
import com.yc.jiushujie.entity.JsjUser;
import com.yc.jiushujie.jsj_user.mapper.JsjUserMapper;
import com.yc.jiushujie.jsj_user.service.impl.JsjUserServiceImpl;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 无罪
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/jiushujie/jsjUser")
public class JsjUserController {
    @Resource
   private JsjUserMapper jsjUserMapper;
    @Resource
    JsjUserServiceImpl jsjUserService;

    @RequestMapping("queryNew")
    public List<JsjUser> queryNew(){
        IPage<JsjUser> page=new Page<>(1,24);
        page.orders().add(OrderItem.desc("reg_time"));
        page=jsjUserMapper.selectPage(page,null);
        return page.getRecords();

    }
    @RequestMapping("queryLastlogin")
    public List<JsjUser> queryLastlogin(){
        IPage<JsjUser> page=new Page<>(1,24);
        page.orders().add(OrderItem.desc("last_login_time"));
        page=jsjUserMapper.selectPage(page,null);
        return page.getRecords();
    }

    @PostMapping("register")
    public Result register( JsjUser jsjUser){
        LambdaQueryWrapper<JsjUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(JsjUser::getAccount,jsjUser.getAccount());
        JsjUser dbuser = jsjUserMapper.selectOne(lqw);
        if (dbuser!=null){
            return Result.failure("用户名已注册!",null);
        }else {
            dbuser = jsjUser;
            jsjUserService.saveOrUpdate(dbuser);
            return Result.success("注册成功!",null);
        }

    }

    //修改头像
    @PostMapping("saveHead")
    Result saveHead(@RequestParam String head,
                    @SessionAttribute Map<String,Object> loginedUser){
        loginedUser.put("head",head);
        JsjUser user = new JsjUser();
        user.setAccount(loginedUser.get("account")+"");
        user.setHeadImg(head);
        int id=jsjUserMapper.selectIdByAccount(loginedUser.get("account")+"");
        user.setId(id);
        jsjUserMapper.updateById(user);
        loginedUser.replace("head_img",head);
        return new Result(1,"头像更新成功",loginedUser.get("head"));
    }

    //修改签名
    @PostMapping("upsign")
    Result upSign(@RequestParam String sign,
                    @SessionAttribute Map<String,Object> loginedUser){
        JsjUser user = new JsjUser();
        user.setAccount(loginedUser.get("account")+"");
        user.setSign(sign);
        int id=jsjUserMapper.selectIdByAccount(loginedUser.get("account")+"");
        user.setId(id);
        jsjUserMapper.updateById(user);
        loginedUser.replace("sign",sign);
        return new Result(1,"签名更新成功",loginedUser.get("sign"));
    }

    //修改密码
    @PostMapping("changepwd")
    Result changePwd(@RequestParam String pwd,
                  @SessionAttribute Map<String,Object> loginedUser){
        JsjUser user = new JsjUser();
        int id=jsjUserMapper.selectIdByAccount(loginedUser.get("account")+"");
        if (!loginedUser.get("pwd").equals(pwd)){
            return new Result(0,"原密码错误",null);
        }
        user.setAccount(loginedUser.get("account")+"");
        user.setPwd(pwd);
        user.setId(id);
        jsjUserMapper.updateById(user);
        loginedUser.replace("pwd",pwd);
        return new Result(1,"密码修改成功",pwd);
    }



    @RequestMapping("queryHotshudian")
    public List<JsjUser> queryHotshudian(){
        IPage<JsjUser> page=new Page<>(1,10);
        page.orders().add(OrderItem.desc("account"));
        page=jsjUserMapper.selectPage(page,null);
        return page.getRecords();

    }
    @RequestMapping("queryHotuser")
    public List<JsjUser> queryHotuser(){
        IPage<JsjUser> page=new Page<>(1,10);
        page.orders().add(OrderItem.desc("name"));
        page=jsjUserMapper.selectPage(page,null);
        return page.getRecords();

    }
    @PostMapping("getownerbooks")
    public JsjUser getownerbooks(@RequestParam String account){




        System.out.println(account);
        System.out.println( jsjUserMapper.selectownerbooks(account));

        return jsjUserMapper.selectownerbooks(account);
    }


    /**
     * 查看个人的书
     * @param id
     * @param page
     * @param size
     * @return
     */
    @PostMapping("getownerbooksBypage")
    public Map<String, Object> getownerbooksBypage(@RequestParam String id,
                                                   @RequestParam int page,
                                                   @RequestParam int size ){
        IPage<JsjBook> iPage=new Page<>(page,size);
        iPage.orders().add(OrderItem.desc("id"));
        int start = (page-1)*size;
        List<JsjBook> jsjBooks = jsjUserMapper.getownerbooksBypage(id, start, size);
        long l = jsjUserMapper.getownerbooksBypageTotal(id);
        Map<String,Object>pageData = new HashMap<>();
        pageData.put("total",l);
        pageData.put("list",jsjBooks);
        return pageData;
    }






    @GetMapping("cart")
    public  Result getCarts(@SessionAttribute Map<String,Object> loginedUser){
     int id= (int) loginedUser.get("id"+"");
     final  JsjUser jsjUser=jsjUserMapper.selectById(id);


        return Result.success("购物车查询成功",jsjUser.getBooks());
    }
}

